﻿using System;
using System.Collections.Generic;
using System.Linq;

using SharedGenomics.Annotations;

namespace SharedGenomics.Workbench.Annotations.Requests
{
    public class PubMedAnnotationRequestStage : IAnnotationRequestStage
    {
        private IAnnotationRepositoryMediator _annotations;

        public PubMedAnnotationRequestStage(IAnnotationRepositoryMediator annotations)
        {
            this._annotations = annotations;
        }

        public void Process(AnnotationRequestContext context, IAnnotationRequestPipelineTraversalStrategy traversalStrategy)
        {
            //try to get the entrez gene id for the current target
            int? entrezGeneId = (int?)this._annotations.GetAnnotationForTarget(AnnotationKey.EntrezGeneId, context.AnnotationTarget);

            if (entrezGeneId.HasValue)
            {
                PubMedSearchAnnotation pubMedAnnotation = new PubMedSearchAnnotation(entrezGeneId.Value);
                context.AddAnnotation(pubMedAnnotation, new AnnotationValue(AnnotationKey.EntrezGeneId, entrezGeneId.Value));
            }
            else
            {
                //fail annotation
                context.FailRequest(AnnotationKey.PubMedSearch);
            }

            //move to next stage
            traversalStrategy.MoveNext(context);
        }
    }
}
